package com.ruicar.afs.cloud.apply.pre.approve.utils;

import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.apply.contract.vo.ContractFileVo;
import com.ruicar.afs.cloud.apply.pre.loan.utils.ImageUtils;
import com.ruicar.afs.cloud.image.config.FileProperties;
import com.ruicar.afs.cloud.image.config.GwtFastdfsProperties;
import com.ruicar.afs.cloud.image.store.feign.ImageStoreApiFeign;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 身份证正反面附件上传工具类
 *
 * @author 耿继辉
 */
@Slf4j
@Component
@AllArgsConstructor
public class ImageUploadUtil {
    private final FileProperties fileProperties;
    private final GwtFastdfsProperties gwtFastdfsProperties;
    private final ImageUtils imageUtil;
    private final ImageStoreApiFeign imageStoreApiFeign;

    /**
     * doUpload for app
     *
     * @param picture-base64
     */
    public Map<String, String> doUpload(String picture, String fileName, Map<String, String> headers) {
        Map<String, String> map = new HashMap<>();
        String outPath = fileProperties.getTempDir() + File.separator + fileName + ".jpg";
        File outFile = new File(outPath);
        FileOutputStream fos = null;
        byte[] reverseIDCardByte = new byte[0];
        try {
            reverseIDCardByte = new BASE64Decoder().decodeBuffer(picture);
            InputStream inputStream = new ByteArrayInputStream(reverseIDCardByte);
            byte[] buf = new byte[2048];
            int len = 0;
            fos = new FileOutputStream(outFile);
            while ((len = inputStream.read(buf)) != -1) {
                fos.write(buf, 0, len);
            }
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(fos);
        }
        MultipartFile mfile = imageUtil.getMulFile(outFile, (fileName + ".jpg"));
        JSONObject resultJson = imageStoreApiFeign.storeFile(gwtFastdfsProperties.getSceneId(), null, mfile, headers);
        if (resultJson.getString("success").equals("1")) {
            JSONObject data = resultJson.getJSONObject("data");
            String md5 = data.getString("md5");
            String suffix = data.getString("suffix");
            String size = data.getString("size");
            Integer isFastDFS = data.getInteger("isFastDFS");
            map.put("md5", md5);
            log.info("md5值:{}", md5);
            map.put("suffix", suffix);
            map.put("size", size);
            map.put("isFastDFS", String.valueOf(isFastDFS));
        }
        log.info("resultJson:{}", resultJson);
        return map;
    }


    /**
     * doUpload  for PC
     *
     * @param mfile-文件流
     */
    public Map<String, String> doUpload(MultipartFile mfile, Map<String, String> headers) {
        Map<String, String> map = new HashMap<>();
        JSONObject resultJson = imageStoreApiFeign.storeFile(gwtFastdfsProperties.getSceneId(), null, mfile, headers);
        if (resultJson.getString("success").equals("1")) {
            JSONObject data = resultJson.getJSONObject("data");
            String md5 = data.getString("md5");
            String suffix = data.getString("suffix");
            String size = data.getString("size");
            Integer isFastDFS = data.getInteger("isFastDFS");
            map.put("md5", md5);
            log.info("md5值:{}", md5);
            map.put("suffix", suffix);
            map.put("size", size);
            map.put("isFastDFS", String.valueOf(isFastDFS));
        }
        log.info("resultJson:{}", resultJson);
        return map;
    }
    /**
     * doUpload  for PC
     *
     * @param mfile-文件流
     */
    public ContractFileVo doUploadNew(MultipartFile mfile, Map<String, String> headers) {
        ContractFileVo contractFileVo=new ContractFileVo();
        JSONObject resultJson = imageStoreApiFeign.storeFile(gwtFastdfsProperties.getSceneId(), null, mfile, headers);
        if (resultJson.getString("success").equals("1")) {
            JSONObject data = resultJson.getJSONObject("data");
            String md5 = data.getString("md5");
            String suffix = data.getString("suffix");
            String size = data.getString("size");
            Integer isFastDFS = data.getInteger("isFastDFS");
            contractFileVo.setFileName(mfile.getOriginalFilename());
            contractFileVo.setSize(size);
            contractFileVo.setIsFastDFS(String.valueOf(isFastDFS));
            contractFileVo.setSuffix(suffix);
            contractFileVo.setMd5(md5);
        }
        return contractFileVo;
    }
    /**
     * 获取token信息
     *
     * @return
     */
    public String getAuthorization() {
        Map<String, String> data = new HashMap<>();
        data.put("client_id", this.gwtFastdfsProperties.getClientId());
        data.put("client_secret", this.gwtFastdfsProperties.getClientSecret());
        data.put("grant_type", this.gwtFastdfsProperties.getGrantType());
        JSONObject jsonObject = this.imageStoreApiFeign.getTokenInfo(data);
        String authorization = jsonObject.getString("token_type") + " " + jsonObject.getString("access_token");
        return authorization;
    }

}
